﻿package mortal.game.scene3D.layer3D
{
    import com.gengine.core.frame.*;
    import flash.utils.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.resource.info.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.player.info.*;

    public class EmbedPlayerLayerBase extends SLayer3D
    {
        private var _globalTimer:FrameTimer;
        private var _globalSecTimer:SecTimer;
        private var _specailEntityList:Array;
        private var _userDisArray:Array;

        public function EmbedPlayerLayerBase(param1:String)
        {
            this._specailEntityList = [];
            this._userDisArray = [];
            super(param1);
            this._globalTimer = new FrameTimer();
            this._globalTimer.addListener(TimerType.ENTERFRAME, this.onTimerFrameHandler);
            this._globalSecTimer = new SecTimer(5);
            this._globalSecTimer.addListener(TimerType.ENTERFRAME, this.onSecTimerHandler);
            return;
        }// end function

        public function run() : void
        {
            this._globalTimer.start();
            this._globalSecTimer.start();
            return;
        }// end function

        public function stop() : void
        {
            this._globalTimer.stop();
            this._globalSecTimer.stop();
            return;
        }// end function

        protected function get allEntitys() : Dictionary
        {
            return null;
        }// end function

        private function onSecTimerHandler(param1:SecTimer) : void
        {
            this.entityTalk();
            return;
        }// end function

        private function onTimerFrameHandler(param1:FrameTimer) : void
        {
            var _loc_2:int = 0;
            if (Game.scene.isInitialize)
            {
                _loc_2 = param1.currentCount % 5;
                switch(_loc_2)
                {
                    case 0:
                    {
                        this.sortEntitys();
                        break;
                    }
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    {
                        this.updateEntity();
                        this.createEntitys();
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            return;
        }// end function

        private function sortEntitys() : void
        {
            if (ThingUtil.isEntitySort)
            {
                this.addAllEntity();
                ThingUtil.isEntitySort = false;
            }
            return;
        }// end function

        private function addAllEntity() : void
        {
            var _loc_2:IEntity = null;
            var _loc_3:UserPlayer = null;
            var _loc_4:MonsterPlayer = null;
            var _loc_5:EntityInfo = null;
            var _loc_6:Boolean = false;
            var _loc_7:Boolean = false;
            var _loc_8:BossInfo = null;
            this._specailEntityList.length = 0;
            this._userDisArray.length = 0;
            var _loc_1:Boolean = false;
            for each (_loc_2 in this.allEntitys)
            {
                
                _loc_5 = _loc_2.entityInfo;
                if (!_loc_5)
                {
                    continue;
                }
                _loc_6 = _loc_5.isDisappear;
                _loc_7 = false;
                if (_loc_2 is MonsterPlayer)
                {
                    _loc_8 = (_loc_2 as MonsterPlayer).bossInfo;
                    if (_loc_8 && ThingUtil.entityUtil.isBossHide(_loc_8.code))
                    {
                        _loc_7 = true;
                    }
                }
                if (SceneRange.isInScene(_loc_2.x2d, _loc_2.y2d) && !_loc_6 && !_loc_7)
                {
                    if (_loc_2 is PetPlayer)
                    {
                        _loc_2.addToStage(this);
                    }
                    else if (_loc_2 is UserPlayer)
                    {
                        _loc_3 = _loc_2 as UserPlayer;
                        _loc_3.addToStage(this);
                        this.addUserToAry(_loc_3);
                    }
                    else if (_loc_2 is MonsterPlayer)
                    {
                        _loc_4 = _loc_2 as MonsterPlayer;
                        _loc_4.addToStage(this);
                    }
                    else
                    {
                        _loc_2.addToStage(this);
                    }
                    continue;
                }
                _loc_2.removeFromStage(this, true);
            }
            return;
        }// end function

        private function removeEntityAll(param1:UserPlayer) : void
        {
            param1.removeFromStage(this, true);
            var _loc_2:* = param1.getSelfPet();
            if (_loc_2)
            {
                _loc_2.removeFromStage(this, true);
            }
            return;
        }// end function

        private function addUserToSpecialArray(param1:UserPlayer) : void
        {
            this._specailEntityList.push(param1);
            return;
        }// end function

        private function addUserToAry(param1:UserPlayer) : void
        {
            this._userDisArray.push(param1);
            return;
        }// end function

        private function createEntitys() : void
        {
            var _loc_1:* = Cache.instance.entity.getFromOffAry();
            while (_loc_1 != null)
            {
                
                ThingUtil.entityUtil.createEntity(_loc_1);
                Cache.instance.entity.removeFromOffAry(_loc_1.entityInfo.entityId);
                _loc_1 = Cache.instance.entity.getFromOffAry();
            }
            return;
        }// end function

        private function updateEntity() : void
        {
            var _loc_1:SpritePlayer = null;
            for each (_loc_1 in this.allEntitys)
            {
                
                _loc_1.updateDisplay();
            }
            return;
        }// end function

        private function entityTalk() : void
        {
            var _loc_1:SpritePlayer = null;
            var _loc_2:MonsterPlayer = null;
            for each (_loc_1 in this.allEntitys)
            {
                
                if (_loc_1 is MonsterPlayer)
                {
                    _loc_2 = _loc_1 as MonsterPlayer;
                    _loc_2.judgeLifeTalk();
                    _loc_2.judgeNormalTalk();
                }
            }
            return;
        }// end function

    }
}
